package presentation;

import java.util.List;
import org.hibernate.*;

import db.*;

public class GetAllAlbumsBean {
	private Session session = null;

	private List<Album> albums;

	private long page;

	public GetAllAlbumsBean() {
		this.session = DbUtils.startSession();
		this.albums = DbUtils.get16Albums(session, 1);
	}

	public GetAllAlbumsBean(int pageNumber) {
		this.session = DbUtils.startSession();
		this.albums = DbUtils.get16Albums(session, pageNumber);
	};

	public List<Album> getPictures() {
		return albums;
	}

	public void releaseResources() {
		if (this.session != null) {
			this.session.close();
		}
	}

	public List<Album> getAllAlbums() {
		this.session = DbUtils.startSession();
		List<Album> albums = DbUtils.getAllAlbums(session);
		return albums;
	}

	public List<Album> getAllExistAlbums() {
		this.session = DbUtils.startSession();
		List<Album> albums = DbUtils.getAllExistAlbums(session);
		return albums;
	}

	public long getAllAlbumsNum() {
		this.session = DbUtils.startSession();
		List<Album> albums = DbUtils.getAllAlbums(session);
		long allAlb = albums.size();
		if (allAlb < 16) {
			page = 1;
		} else {
			if (allAlb % 16 != 0) {
				page = (allAlb / 16) + 1;
			} else {
				page = (allAlb / 16);
			}
		}
		return page;
	}

	public long getAllAlbumsNumbyUser(long userId) {
		this.session = DbUtils.startSession();
		List<Album> albums = DbUtils.getUserAlbums(session, userId);
		long allAlb = albums.size();
		if (allAlb < 16) {
			page = 1;
		} else {
			if (allAlb % 16 != 0) {
				page = (allAlb / 16) + 1;
			} else {
				page = (allAlb / 16);
			}
		}
		return page;
	}

	public List<Album> getAlbums() {
		return albums;
	}

	public List<Album> get16CildAlbumsByParentId(long parentId, int pageNumber ) {
		this.session = DbUtils.startSession();
		List<Album> childAlbums = DbUtils.get16ChildAlbumsByParent(session, parentId, pageNumber);
		return childAlbums;
	}
	
	public long get16ChildAlbumsNum(long parentId) {
		this.session = DbUtils.startSession();
		Album album=DbUtils.findAlbum(session, parentId);
		List<Album> childAlbums = album.getChildAlbums();
		long allAlb = childAlbums.size();
		if (allAlb < 16) {
			page = 1;
		} else {
			if (allAlb % 16 != 0) {
				page = (allAlb / 16) + 1;
			} else {
				page = (allAlb / 16);
			}
		}
		return page;
	}
	
	public List<Picture> getAllPictureByAlbum(long albumId) {
		this.session = DbUtils.startSession();
		List<Picture> pictures = DbUtils.getAllPicturesByAlbum(session, albumId);	
		return pictures;
	}
	
	public  List<Album> getUserAlbums(long userId) {
		 this.session = DbUtils.startSession();
		 List<Album> albums=DbUtils.getUserAlbums(session, userId);
		 return albums;
	}
	
	public  boolean isUserAlbum(long userId, long albumId) {
		 this.session = DbUtils.startSession();
		 boolean bool=DbUtils.isUserAlbum(session, userId, albumId);
		 return bool; 
	}
}
